package com.neusoft.service.impl;

import com.neusoft.dto.MedicalTechnologyDto;
import com.neusoft.entity.CheckRequest;
import com.neusoft.entity.DisposalRequest;
import com.neusoft.entity.InspectionRequest;
import com.neusoft.mapper.*;
import com.neusoft.service.CheckRequestService;
import com.neusoft.service.DisposalRequestService;
import com.neusoft.service.InspectionRequestService;
import com.neusoft.service.MedicalTechnologyService;
import com.neusoft.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author akemihomurasama
 */
@Service
public class MedicalTechnologyServiceImpl implements MedicalTechnologyService {
    @Autowired
    private CheckRequestService checkRequestService;
    @Autowired
    private InspectionRequestService inspectionRequestService;
    @Autowired
    private DisposalRequestService disposalRequestService;
    @Autowired
    private MedicalTechnologyMapper medicalTechnologyMapper;
    @Autowired
    private MedicalTechnologyDtoMapper medicalTechnologyDtoMapper;
    @Autowired
    private CheckRequestMapper checkRequestMapper;
    @Autowired
    private DisposalRequestMapper disposalRequestMapper;
    @Autowired
    private InspectionRequestMapper inspectionRequestMapper;
    /**
     * 查询医技项目数量，根据分类查询
     *
     * @param techType 医技项目分类
     * @return 数量
     */
    @Override
    public Result queryTecNum(String techType) {
        return Result.ok(medicalTechnologyMapper.queryTecNum(techType));
    }

    /**
     * 根据分类查询医技项目列表
     *
     * @param techType 分类
     * @return 列表
     */
    @Override
    public Result queryTechByType(String techType) {
        return Result.ok(medicalTechnologyMapper.queryTechByType(techType));
    }

    /**
     * 根据病人id和医技项目id查询医技项目列表
     *
     * @param registerId 医技项目id列表
     * @return 医技项目列表
     */
    @Override
    public Result queryAllTechByPatient(int registerId) {
        //todo 当患者重名时要让前端判断
        List<MedicalTechnologyDto> medicalTechnologyDto1 = medicalTechnologyDtoMapper.queryCheckTechWithTime(registerId, "已开立");
        List<MedicalTechnologyDto> medicalTechnologyDto2 = medicalTechnologyDtoMapper.queryInspectionTechWithTime(registerId, "已开立");
        List<MedicalTechnologyDto> medicalTechnologyDto3 = medicalTechnologyDtoMapper.queryDisposalTechWithTime(registerId, "已开立");
        List<MedicalTechnologyDto> allList = new ArrayList<>(medicalTechnologyDto1);
        allList.addAll(medicalTechnologyDto2);
        allList.addAll(medicalTechnologyDto3);
        return Result.ok(allList);
    }

    /**
     * 根据患者id查询医技项目和对应的申请的状态
     *
     * @param registerId 医技项目
     * @return 查询结果
     */
    @Override
    public Result queryTechWithState(int registerId) {
        List<MedicalTechnologyDto> medicalTechnologyDto1 = medicalTechnologyDtoMapper.queryCheckTechListByRegisterId(registerId);
        List<MedicalTechnologyDto> medicalTechnologyDto2 = medicalTechnologyDtoMapper.queryDisposalTechListByRegisterId(registerId);
        List<MedicalTechnologyDto> medicalTechnologyDto3 = medicalTechnologyDtoMapper.queryInspectionTechListByRegisterId(registerId);
        List<MedicalTechnologyDto> allList = new ArrayList<>(medicalTechnologyDto1);
        allList.addAll(medicalTechnologyDto2);
        allList.addAll(medicalTechnologyDto3);
        return Result.ok(allList);
    }

    /**
     * 获得患者已缴费的申请
     *
     * @param registerId 患者id
     * @return 修改结果
     */
    @Override
    public Result queryPayedTech(int registerId) {
        List<MedicalTechnologyDto> medicalTechnologyDto1 = medicalTechnologyDtoMapper.queryCheckTechWithTime(registerId, "已缴费");
        List<MedicalTechnologyDto> medicalTechnologyDto2 = medicalTechnologyDtoMapper.queryInspectionTechWithTime(registerId, "已缴费");
        List<MedicalTechnologyDto> medicalTechnologyDto3 = medicalTechnologyDtoMapper.queryDisposalTechWithTime(registerId, "已缴费");
        List<MedicalTechnologyDto> allList = new ArrayList<>(medicalTechnologyDto1);
        allList.addAll(medicalTechnologyDto2);
        allList.addAll(medicalTechnologyDto3);
        return Result.ok(allList);
    }

    /**
     * 根据已缴费和患者id查询申请列表
     *
     * @param registerId 患者id
     * @param state      申请状态
     * @return 申请列表
     */
    @Override
    public Result queryTechByStateWithRequestId(int registerId, String state) {
        List<MedicalTechnologyDto> list1 = medicalTechnologyDtoMapper.queryTechByIdListWithDisposalRequestId(registerId, state);
        List<MedicalTechnologyDto> list2 = medicalTechnologyDtoMapper.queryTechByIdListWithInspectionRequestId(registerId, state);
        List<MedicalTechnologyDto> list3 = medicalTechnologyDtoMapper.queryTechByIdListWithCheckRequestId(registerId, state);
        List<MedicalTechnologyDto> allList = new ArrayList<>();
        allList.addAll(list1);
        allList.addAll(list2);
        allList.addAll(list3);
        return Result.ok(allList);
    }

    /**
     * 同时根据申请id修改申请状态为已退费
     *
     * @param medicalTechnologyDtoList 申请列表
     * @return 修改结果
     */
    @Override
    @Transactional
    public Result techGiveUpMoney(List<MedicalTechnologyDto> medicalTechnologyDtoList) {
        List<Integer> checkIdList = new ArrayList<>();
        List<Integer> inspectionIdList = new ArrayList<>();
        List<Integer> disposalIdList = new ArrayList<>();
        medicalTechnologyDtoList.forEach(medicalTech -> {
            if (medicalTech.getCheckRequestId() != null) {
                checkIdList.add(medicalTech.getCheckRequestId());
            }
            if (medicalTech.getDisposalRequestId() != null) {
                disposalIdList.add(medicalTech.getDisposalRequestId());
            }
            if (medicalTech.getInspectionRequestId() != null) {
                inspectionIdList.add(medicalTech.getInspectionRequestId());
            }
        });
        if(checkIdList.size()>0){
            checkRequestMapper.updateReqByIdList(checkIdList,"已退款");
        }
        if(inspectionIdList.size()>0){
            inspectionRequestMapper.updateReqByIdList(inspectionIdList,"已退款");
        }
        if(disposalIdList.size()>0){
            disposalRequestMapper.updateReqByIdList(disposalIdList,"已退款");
        }
        return Result.ok();
    }

}
